package com.itany.corejava.code07_面向对象.bms;

import java.util.Scanner;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年11月08日 11:21
 */
public class AccountMenu {
    private Scanner sc=new Scanner(System.in);
    private Account account;//登录成功后必须将那个成功的账户作为参数传递进来

    public AccountMenu(Account account) {
        this.account = account;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }

    public void showMainMenu(){
        System.out.println("-----------------欢迎进入银行系统的主菜单------------------");
        System.out.print("请选择服务(1.存款 2.取款 3.查询余额 4.转账 5.贷款 6.还款 7.查询贷款金额 8.修改密码 9.退出)");
        int choice=sc.nextInt();
        switch (choice){
            case 1:
                deposit();
                break;
            case 2:
                withdrawMoney();
                break;
            case 3:
                queryBalance();
                break;
            case 4:
                transfer();
                break;
            case 5:
                providerLoan();
                break;
            case 6:
                payLoan();
                break;
            case 7:
                queryLoan();
                break;
            case 8:
                modifyPassword();
                break;
            case 9:
                loginOut();
                break;
        }

    }
    //存款方法
    public void deposit(){
        System.out.println("-------开始存款-------");
        System.out.print("请输入存款的金额:");
        double money=sc.nextDouble();
        account.deposit(money);
        System.out.println("恭喜您，存款成功啦！");
        showMainMenu();
    }


    //取款
    public void withdrawMoney(){
        //提示用输入取款金额
        System.out.println("-------开始存取款-------");
        System.out.print("请输入您要取的金额:");
        double money=sc.nextDouble();
        boolean flag=account.withdrawMoney(money);
        if(flag){
            System.out.println("恭喜您，取款成功啦");
        }else{
            System.out.println("取款失败啦~~~");
        }
        showMainMenu();

    }
    //查询余额
    public void queryBalance(){
        System.out.println("--------查询余额--------");
        System.out.println("您当前账户余额为:"+account.getBalance()+"元");
        showMainMenu();
    }

    //转账
    public void transfer(){
        System.out.println("-----------开始转账------------");
        System.out.print("请输入您要转入的目标账户的编号:");
        int id=sc.nextInt();
        //根据输入的id查找是否有该账号
        Account targetAccount=Bank.findAccountById(id);
        if(targetAccount==null){
            System.out.println("不好意思，您要转入的账户是不存在的");
        }else{
            //如果存在目标账号，则执行转账功能
            System.out.print("请输入转账的金额:");
            double money=sc.nextDouble();
            if(account.getBalance()>=money){//当前余额>=转账金额
                account.setBalance(account.getBalance()-money);//当前账户余额减少
                targetAccount.setBalance(targetAccount.getBalance()+money);//目标余额增加
            }else{
                System.out.println("对不起，您的余额不够!!!");
            }

        }

        showMainMenu();

    }

    //贷款方法
    public void providerLoan(){
        System.out.println("----------开始贷款-----------");

        //所有的贷款账户都实现了Loanable接口
        if(account instanceof  Loanable){
            System.out.println("请输入贷款金额:");
            double money=sc.nextDouble();

            //贷款方法只有Loanable接口中有，可以通过对应的实现类提供
            // 面向接口编程，接口指向实例，多态的写法
            Loanable loanable=(Loanable)account;

            //执行贷款方法
            loanable.providerLoan(money);
            System.out.println("恭喜您,贷款成功,您当前的贷款金额是:"+loanable.queryLoan());

        }else{
            System.out.println("不好意思，您的账户不是贷款账户，无法操作贷款功能");
        }
        showMainMenu();
    }

    //还贷款
    public void payLoan(){
        System.out.println("---------开始还贷款--------");
        if(account instanceof  Loanable){
            Loanable loanable=(Loanable)account;
            System.out.println("请输入您需要还贷款的金额:");
            double money=sc.nextDouble();
            boolean flag=loanable.payLoan(money);
            if(flag){
                System.out.println("恭喜您，还贷成功，当前账户余额是:"+account.getBalance()+",当前贷款总额:"+loanable.queryLoan());
            }else{
                System.out.println("不好意思，您的账户不是贷款账户，无法操作还贷功能！！！");
            }
        }
        showMainMenu();
    }

    //查询贷款金额
    public void queryLoan(){
        System.out.println("------------查询贷款金额-------------");
        if(account instanceof  Loanable){
            Loanable loanable=(Loanable)account;
            System.out.println("您好，你的当前贷款的总额是:"+loanable.queryLoan());
        }else{
            System.out.println("不好意思，您的账户不是贷款账户，无法查看贷款余额");
        }
        showMainMenu();
    }

    public void modifyPassword(){
        System.out.println("----------修改密码----------");
        System.out.println("请输入原密码:");
        String oldPassword=sc.next();
        if(oldPassword.equals(account.getPassword())){

            while(true){
                System.out.println("请输入新的密码:");
                String newPassword=sc.next();
                System.out.println("请再次输入新的密码:");
                String newRepassWord=sc.next();
                if(newPassword.equals(newRepassWord)){
                    account.setPassword(newPassword);
                    System.out.println("恭喜您，密码设置成功!!!");
                    Start start=new Start();
                    start.showMenu();
                    // break;
                }else{
                    System.out.println("不好意思，两次密码输入不一致，请重新输入");

                }
            }

        }else{
            System.out.println("您输入的原密码不正确，请重新输入");
            modifyPassword();
        }
    }

    public void loginOut(){
        System.out.println("退出成功");
        new Start().showMenu();
    }



}
